/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Timer;
import file.Configurable;
import file.Configuration;

/**
 *
 * @author Mentors
 */
public class Minibot implements Configurable{

    private static Manipulator    manipulator;
    private static Timer          deployTimer;

    // Don't allow the operator to deploy the minibot until the last 10 seconds
    private static int            deployLockoutSeconds = 110;

    private static DoubleSolenoid minibotSolenoid;

    private static Minibot        m_instance = null;


    /**
     *  Get an instance of the Minibot
     */
    public static Minibot getInstance() {

        if (m_instance == null) {
            m_instance = new Minibot();
            Configuration.getInstance().register("Minibot", m_instance);
        }
        
        return m_instance;
    }

    private Minibot() {

        deployTimer     = new Timer();

//        minibotSolenoid = new DoubleSolenoid(LogomotionRobot.kDeployMiniForward,
//                                             LogomotionRobot.kDeployMiniReverse);
//
//        minibotSolenoid.set(DoubleSolenoid.Value.kReverse);  // this may need to be kForward

        manipulator = Manipulator.getInstance();
    }

    /**
     * Called from teleopInit to ensure the minibot cannot be deployed
     * until allowed.
     */
    public void startTimer() {

        deployTimer.reset();
        deployTimer.start();
    }

    /**
     * Called from teleopPeriodic to check if the operator tried to
     *  deploy the minibot.
     */
    public void checkDeployment(boolean deployMinibotButtonState) {

        if (deployMinibotButtonState) {

            // if in the last 10 seconds of the teleop phase, allow the minibot to be deployed

            if (deployTimer.get() >= deployLockoutSeconds) { // must be 110 for competition

                // button was pressed to deploy the minibot; initiate deployment
                manipulator.releaseMinibot();

                System.out.println(deployTimer.get() + " Minibot deployed.");
            }
            else {

                System.out.println(deployTimer.get() + " Minibot deployment button ignored.");
            }
        }
        else {
            // do nothing - button not pressed
        }
    } /* checkDeployment */

    public void update(String name, String[] values) {
        if(name.equals("Lockout.Seconds")){
            int val = Integer.parseInt(values[0]);
            deployLockoutSeconds = val;
            System.out.println("Minibot lockout: " + val);
        }
    }

}